package com.xigua.xcar;

import android.app.Application;
import android.location.Location;
import android.util.Log;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;

public class BMapApiDemoApp extends Application {

	static BMapApiDemoApp mDemoApp;
	private LocationUpdateListener mListener = null; 
	public BMapManager mBMapMan = null;
	public boolean mIsDevelopMode = false;
	public String mStrKey = "B699C180B5BDAF7AAF2DE8F4EDE9E7405F52E0CE";
	private static final String tag = "BMapApiDemoApp";
	private MKSearch mSearch = new MKSearch();
	LocationListener mLocationListener = null;//create时注册此listener，Destroy时需要Remove
	boolean m_bKeyRight = true; 
	private MKAddrInfo mAddrInfo = null;
	
	public String getLocationAddress(){
		if(null != mAddrInfo){
			return mAddrInfo.strAddr;
		}else{
			return null;
		}
	}
	
	public void setLocationUpdateListener(LocationUpdateListener listener){
		mListener = listener;
	}
	
	static public class MyGeneralListener implements MKGeneralListener {
		public void onGetNetworkState(int iError) {
		}

		public void onGetPermissionState(int iError) {
			Log.d("MyGeneralListener", "onGetPermissionState error is "
					+ iError);
			if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
				BMapApiDemoApp.mDemoApp.m_bKeyRight = false;
			}
		}
	}

	@Override
	public void onCreate() {
		Log.v("BMapApiDemoApp", "onCreate");
		mDemoApp = this;
		if(false==mIsDevelopMode){
			mBMapMan = new BMapManager(this);
			mBMapMan.init(this.mStrKey, new MyGeneralListener());
			mBMapMan.getLocationManager().setNotifyInternal(10, 5);
			mBMapMan = new BMapManager(this);
			mBMapMan.init(mStrKey, new BMapApiDemoApp.MyGeneralListener());
			mSearch.init(mBMapMan, new MKSearchListener() {
				
				public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) {
				}
				
				public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
					// TODO Auto-generated method stub
					
				}
				
				public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
					// TODO Auto-generated method stub
					
				}
				
				public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) {
					Log.i(tag, "onGetDrivingRouteResult");
					if(null != mListener){
						mListener.onGetDrivingRouteResult(res, error);
					}
				}
				
				public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
					// TODO Auto-generated method stub
					
				}
				
				public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
					if(arg0==null){
						Log.i(tag,"onGetAddrResult:address:!!!null!!!");
					}else{
						if(null == mAddrInfo){
							mAddrInfo = arg0;
							Log.i(tag,"onGetAddrResult:address:"+mAddrInfo.strAddr);
						}else{
							if(mAddrInfo.strAddr.equals(arg0.strAddr)){
								return ;
							}
							mAddrInfo = arg0;
							Log.i(tag,"onGetAddrResult:address:"+mAddrInfo.strAddr);
						}
						if(null != mListener){
							mListener.notifyLocationUpdate(mAddrInfo.strAddr);
						}
					}
				}

				@Override
				public void onGetSuggestionResult(MKSuggestionResult arg0,
						int arg1) {
					// TODO Auto-generated method stub
					
				}
			});
			
			mLocationListener = new LocationListener(){
				
				public void onLocationChanged(Location location) {
					if(location != null){
						String strLog = String.format("您当前的位置:\r\n" +
								"纬度:%f\r\n" +
								"经度:%f",
								location.getLongitude(), location.getLatitude());
						Log.i(tag,strLog);
						RoutePlan.setLocation(location);
						int longitude = (int) (1000000 * location.getLongitude());  
	                    int latitude = (int) (1000000 * location.getLatitude());  
	  
	                    // 查询该经纬度值所对应的地址位置信息   
	                    mSearch.reverseGeocode(new GeoPoint(latitude, longitude));  
					}else{
						Log.i(tag,"!!!null!!!");
					}
					if(null != mListener){
						mListener.onLocationChanged(location);
					}
				}
				
				
	        };
		}
		mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
		mBMapMan.start();
		super.onCreate();
	}

	public void drivingSearch(String arg0, MKPlanNode arg1, String arg2, MKPlanNode arg3){
		if(null != mSearch){
			mSearch.drivingSearch(arg0, arg1, arg2, arg3);
		}
	}
	
	@Override
	public void onTerminate() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
	}

}
